package com.wslds.blog.action.front;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedOutput;
import com.wslds.base.dao.page.PageAdapter;
import com.wslds.base.util.BlogCons;
import com.wslds.base.util.FunctionUtil;
import com.wslds.base.util.SessionUtil;
import com.wslds.base.util.Strings;
import com.wslds.bean.Type;
import com.wslds.bean.ext.ArticleExt;
import com.wslds.bean.ext.TaginstExt;
import com.wslds.blog.action.BaseArticleAction;

@SuppressWarnings("serial")
@Component("articleAction")
@Scope("prototype")
public class ArticleAction extends BaseArticleAction {

	@SuppressWarnings("all")
	public String list() {
		this.setQueryString(this.getHttpRequest().getQueryString());

		Map<Long, Type> typeMap = (Map<Long, Type>) SessionUtil
				.getGlobe(BlogCons.APPLICATION.TYPE);
		Map<Long, String> tagMap = (Map<Long, String>) SessionUtil
				.getGlobe(BlogCons.APPLICATION.TAG);
		try {
			if (null != articleExt) {
				if (null != articleExt.getTypeId()) {
					this.put("titleApart",
							"类型:"
									+ typeMap.get(articleExt.getTypeId())
											.getName() + " - ");
				} else if (Strings.notNull(articleExt.getTags())) {
					this.put(
							"titleApart",
							"标签:"
									+ tagMap.get(Long.valueOf(articleExt
											.getTags())) + " - ");
				} else if (Strings.notNull(articleExt.getDates())) {
					this.put("titleApart", "日期:" + articleExt.getDates()
							+ " - ");
				}
			}
		} catch (NumberFormatException e) {

			return "error404";
		}

		return SUCCESS;
	}

	@SuppressWarnings("all")
	public String listAjax() {

		if (null == articleExt) {
			articleExt = new ArticleExt();
		}
		articleExt.setStat("MEN");
		articleExt.setExtraOrderColumns(" id desc ");

		if (this.getHttpRequest().getQueryString() != null) {
			this.setQueryString(Strings.cutPageQueryString(this
					.getHttpRequest().getQueryString()));
		}

		if (Strings.notNull(articleExt.getTags())) {
			try {
				Long.valueOf(articleExt.getTags());
			} catch (NumberFormatException e) {
				return "error404";
			}
		}

		Map<Long, List<TaginstExt>> taginstMap = (Map<Long, List<TaginstExt>>) SessionUtil
				.getGlobe(BlogCons.APPLICATION.TAGINST);
		Map<Long, Type> typeMap = (Map<Long, Type>) SessionUtil
				.getGlobe(BlogCons.APPLICATION.TYPE);

		PageAdapter<ArticleExt> adapter = service.pageExt("article_select_ext",
				articleExt, getPage());
		this.put("adapter", adapter);

		for (ArticleExt ext : adapter.getData()) {
			ext.setTagList(taginstMap.get(ext.getId()));
			ext.setType(typeMap.get(ext.getTypeId()));

			String pureData = FunctionUtil.Html2Text(ext.getContent());

			int blogStart = ext.getContent().indexOf(BlogCons.BLOG_SPLIT.START);
			int blogEnd = ext.getContent().indexOf(BlogCons.BLOG_SPLIT.END);
			if (blogStart == -1 || blogEnd == -1) {
				ext.setContent(FunctionUtil.abbreviate(pureData, 500, "..."));
			} else {
				ext.setContent(ext.getContent().substring(blogStart, blogEnd)
						+ "........");
			}
		}

		return SUCCESS;
	}

	@SuppressWarnings("all")
	public String rss() throws Exception {

		if (null == articleExt) {
			articleExt = new ArticleExt();
		}
		articleExt.setStat("MEN");
		articleExt.setExtraOrderColumns(" id desc ");

		if (this.getHttpRequest().getQueryString() != null) {
			this.setQueryString(Strings.cutPageQueryString(this
					.getHttpRequest().getQueryString()));
		}

		Map<Long, List<TaginstExt>> taginstMap = (Map<Long, List<TaginstExt>>) SessionUtil
				.getGlobe(BlogCons.APPLICATION.TAGINST);
		Map<Long, Type> typeMap = (Map<Long, Type>) SessionUtil
				.getGlobe(BlogCons.APPLICATION.TYPE);

		PageAdapter<ArticleExt> adapter = service.pageExt("article_select_ext",
				articleExt, getPage(), 100);
		this.put("adapter", adapter);

		for (ArticleExt ext : adapter.getData()) {
			ext.setTagList(taginstMap.get(ext.getId()));
			ext.setType(typeMap.get(ext.getTypeId()));
		}

		SyndFeed feed = Strings.genRss(adapter.getData());
		this.getResponse().setContentType("text/xml");
		this.getResponse().setCharacterEncoding("UTF-8");
		PrintWriter pw = this.getResponse().getWriter();
		SyndFeedOutput output = new SyndFeedOutput();
		output.output(feed, pw);

		return NONE;
	}

	@SuppressWarnings("all")
	public String show() {

		if (null == articleExt) {
			return REDRICT_TO_LIST;
		}

		if (StringUtils.isNumeric(articleExt.getIds())) {
			articleExt.setStat("MEN");
			articleExt.setId(Long.valueOf(articleExt.getIds()));

			articleExt = service.findOneExt("article_select_ext", articleExt);
			if (null == articleExt) {
				return "err";
			} else {
				articleExt.setViewSum(articleExt.getViewSum() + 1);
				service.update("article", articleExt);
			}

			String pureData = FunctionUtil.Html2Text(articleExt.getContent());
			articleExt.setContent4Simple(FunctionUtil.abbreviate(pureData, 150,
					"..."));

			Map<Long, List<TaginstExt>> tagMap = (Map<Long, List<TaginstExt>>) SessionUtil
					.getGlobe(BlogCons.APPLICATION.TAGINST);
			Map<Long, Type> typeMap = (Map<Long, Type>) SessionUtil
					.getGlobe(BlogCons.APPLICATION.TYPE);
			articleExt.setTagList(tagMap.get(articleExt.getId()));
			articleExt.setType(typeMap.get(articleExt.getTypeId()));

			List<ArticleExt> neiList = service.getNeiWeb(articleExt);
			this.put("neiList", neiList);

		} else {
			// 错误去处
			return "error404";
		}

		return SUCCESS;
	}

}
